<?php
namespace app\controllers;

use app\commission\PaidFinish;
use app\controllers\pay\PayRefund;
use app\controllers\wx\services\Template;
use app\models\City;
use app\models\City4;
use app\models\ExpressCallback;
use app\models\order\Order;
use app\models\pay\Pay;
use app\models\Queue;
use app\models\SetUserUp;
use app\models\system\SetApp;
use app\models\user\UserLevel;
use app\models\ZRegion;
use app\services\Chain;
use app\util\Request;
use yii\helpers\ArrayHelper;

class DemoController extends Controller
{
    // 用这个
    public function actionIndex()
    {
        $list = City4::find()
            ->where(['pid' => 0])
            ->orderBy(['id' => SORT_ASC])
            // ->limit(3)
            ->all();

        foreach ($list as $p) {
            $province = ZRegion::findOne(['id' => $p->id]);
            if (!$province) {
                $province = new ZRegion();
                $province->name = $p->name;
                $province->id = $p->id;
                $province->save();
            }

            $old_city = City4::find()
                ->where(['pid' => $province->id])
                ->orderBy(['id' => SORT_ASC])
                ->all();
            foreach ($old_city as $c) {
                $city = ZRegion::findOne(['id' => $c->id]);
                if (!$city) {
                    $city = new ZRegion();
                    $city->name = $c->name;
                    $city->id = $c->id;
                    $city->p_code = $province->code;
                    $city->save();
                }

                $old_area = City4::find()
                    ->where(['pid' => $city->id])
                    ->orderBy(['id' => SORT_ASC])
                    ->all();

                foreach ($old_area as $a) {
                    $area = ZRegion::findOne(['id' => $a->id]);
                    if (!$area) {
                        $area = new ZRegion();
                        $area->name = $a->name;
                        $area->id = $a->id;
                        $area->p_code = $city->code;
                        $area->save();
                    }
                }
            }

        }

        echo 'ok';
    }

    public function actionCity()
    {
        $list = City::find()
            ->where(['city_id' => 0])
            // ->andWhere(['id' => [110000, 120000, 310000, 500000]])
            ->orderBy(['id' => SORT_ASC])
            // ->limit(4)
            ->all();
        // var_dump($list);

        foreach ($list as $p) {
            $province = ZRegion::findOne(['id' => $p->id]);
            if (!$province) {
                $province = new ZRegion();
                $province->name = $p->name;
                $province->id = $p->id;
                $province->save();
            }

            if (!in_array($province->id, [110000, 120000, 310000, 500000])) {
                $old_city = City::find()
                    ->where(['province_id' => $province->id])
                    ->andWhere(['>', 'city_id', 0])
                    ->andWhere(['area_id' => 0])
                    ->orderBy(['id' => SORT_ASC])
                    ->asArray()
                    ->all();
            } else {
                $old_city = [
                    0 => [
                        'name' => '直辖市',
                        // 'province_id' => $province->id,
                        'id' => $province->id + 1,
                    ]
                ];
            }

            // var_dump($old_city);

            foreach ($old_city as $c) {
                $city = ZRegion::findOne(['id' => $c['id'], 'name' => $c['name']]);
                if (!$city) {
                    $city = new ZRegion();
                    $city->name = $c['name'];
                    $city->id = $c['id'];
                    $city->p_code = $province->code;
                    $rs = $city->save();
                    // if (!$rs) {
                    //     var_dump($city->getFirstErrors());exit;
                    // }
                }


                if (!in_array($province->id, [110000, 120000, 310000, 500000])) {
                    $old_area = City::find()
                        ->where(['city_id' => $city->id])
                        ->andWhere(['>', 'area_id', 0])
                        ->orderBy(['id' => SORT_ASC])
                        ->all();
                } else {
                    $old_area = City::find()
                        ->where(['province_id' => $province->id])
                        ->andWhere(['>', 'city_id', 0])
                        ->andWhere(['area_id' => 0])
                        ->orderBy(['id' => SORT_ASC])
                        ->all();
                }

                foreach ($old_area as $a) {
                    $area = ZRegion::findOne(['id' => $a->id]);
                    if (!$area) {
                        $area = new ZRegion();
                        $area->name = $a->name;
                        $area->id = $a->id;
                        $area->p_code = $city->code;
                        $area->save();
                    }
                }

            }

        }

        echo 'ok';
    }


    public function actionCall()
    {
        // $order = Order::findOne(['id' => 27]);
        //
        // $call = ExpressCallback::findOne(['waybill' => $order->waybill, 'shopbill' => $order->shopbill]);
        //
        // var_dump($call->typeCode);exit;

        $order = Order::findOne(['id' => 28]);
        var_dump($order->add_freight <= 0 || ($order->add_freight > 0 && $order->add_freight_ok == 1));

    }

    public function actionAdd()
    {
        // $sn = '2025022111370643982';
        // $pay = Pay::findOne(['sn' => $sn]);
        //
        // PaidFinish::paidRun($pay);

        // PayRefund::refund(62);

        // Template::expressOrder(Order::findOne(['id' => 62]));

        Template::commission(Order::findOne(['id' => 1]), 100, 11195);
    }
}